<?php
require_once ("person_controller.php");
class Suppliers extends Person_controller
{
	function __construct()
	{
		parent::__construct('suppliers');
	}
	
	function index()
	{
		$data['controller_name']=strtolower($this->uri->segment(1));
		$data['form_width']=$this->get_form_width();
		$data['manage_table']=suppliers_people_manage_table($this->Supplier->get_all(),$this);
		$this->load->view('people/supplier_manage',$data);
	}
	
	/*
	Returns supplier table data rows. This will be called with AJAX.
	*/
	function search()
	{
		$search=$this->input->post('search');
		$data_rows=suppliers_people_manage_table_data_rows($this->Supplier->search($search),$this);
		echo $data_rows;
	}
	
	/*
	Gets one row for a person manage table. This is called using AJAX to update one row.
	*/
	function get_row_supplier()
	{
		$person_id = $this->input->post('row_id');
		$data_row=suppliers_person_data_row($this->Person->get_info($person_id),$this);
		echo $data_row;
	}
	
	/*
	Gives search suggestions based on what is being searched for
	*/
	function suggest()
	{
		$suggestions = $this->Supplier->get_search_suggestions($this->input->post('q'),$this->input->post('limit'));
		echo implode("\n",$suggestions);
	}
	
	/*
	Loads the supplier edit form
	*/
	function view($supplier_id=-1)
	{
		$data['person_info']=$this->Supplier->get_info($supplier_id);
		$this->load->view("suppliers/form",$data);
	}
	
	/*
	Inserts/updates a supplier
	*/
	function save($supplier_id=-1)
	{
		$person_data = array(
		'first_name'=>$this->input->post('first_name'),
		'last_name'=>$this->input->post('last_name'),
		'email'=>$this->input->post('email'),
		'phone_number'=>$this->input->post('phone_number'),
		'address_1'=>$this->input->post('address_1'),
		'address_2'=>$this->input->post('address_2'),
		'city'=>$this->input->post('city'),
		'state'=>$this->input->post('state'),
		//'zip'=>$this->input->post('zip'),
		'zip'=>'',
		'country'=>$this->input->post('country'),
		'comments'=>$this->input->post('comments')
		);
		$supplier_data=array(
		'account_number'=>$this->input->post('account_number')=='' ? null:$this->input->post('account_number'),
		);
		if($this->Supplier->save($person_data,$supplier_data,$supplier_id))
		{
			//New supplier
			if($supplier_id==-1)
			{
				echo json_encode(array('success'=>true,'message'=>$this->lang->line('suppliers_successful_adding').' '.
				$person_data['first_name'].' '.$person_data['last_name'],'person_id'=>$supplier_data['person_id']));
			}
			else //previous supplier
			{
				echo json_encode(array('success'=>true,'message'=>$this->lang->line('suppliers_successful_updating').' '.
				$person_data['first_name'].' '.$person_data['last_name'],'person_id'=>$supplier_id));
			}
		}
		else//failure
		{	
			echo json_encode(array('success'=>false,'message'=>$this->lang->line('suppliers_error_adding_updating').' '.
			$person_data['first_name'].' '.$person_data['last_name'],'person_id'=>-1));
		}
	}
	
	/*
	This deletes suppliers from the suppliers table
	*/
	function delete()
	{
		$suppliers_to_delete=$this->input->post('ids');
		
		if($this->Supplier->delete_list($suppliers_to_delete))
		{
			echo json_encode(array('success'=>true,'message'=>$this->lang->line('suppliers_successful_deleted').' '.
			count($suppliers_to_delete).' '.$this->lang->line('suppliers_one_or_multiple')));
		}
		else
		{
			echo json_encode(array('success'=>false,'message'=>$this->lang->line('suppliers_cannot_be_deleted')));
		}
	}
	
	/*
	get the width for the add/edit form
	*/
	function get_form_width()
	{			
		return 360;
	}
	
	
	function get_account($supplier_id=-1)
	{
		$sortable_data = array();
		$supplier_orders = $this->Supplier->get_supplier_orders($supplier_id);
		foreach($supplier_orders->result() as $row)
		{	
			$raw_data = array();
			$order_id = $row->order_id;
			$supplier_id = $row->supplier_id;
			$order_date = $row->order_date;
			$employee_id = $row->ordered_by;
			
			//Get the employee info
			$employee_info = $this->Employee->get_info($employee_id);
		
			//Get the total cost of order
			$items_result = $this->Item->get_orders_totals($order_id);
			$output = array();
			foreach($items_result->result() as $row)
			{
				$output[]=$row->item_total;
			}
			$sum = number_format(array_sum($output), 2, '.', '');
			
			///////Get the total payment made//////
			$payment_result = $this->Item->get_orders_payment_totals($order_id);
			if($payment_result->num_rows() == 0)
			{
				$payment = 0;
			}
			else
			{
				$payment_row = $payment_result->row();
				$payment = $payment_row->pay_total;
				if(empty($payment))
				{
					$payment = 0;
				}
			}
				
			$payment = number_format($payment, 2, '.', '');
			
			/////////Now the Balance/////////////
			$balance = number_format($sum - $payment, 2, '.', '');
		
			
			$employee_name = $employee_info->first_name.' '.$employee_info->last_name;
			if(empty($employee_name))
			{
				$employee_name = '[None]';
			}
			
			$raw_data[] = $order_id;
			$raw_data[] = $order_date;
			$raw_data[] = $employee_name;
			$raw_data[] = $sum;
			$raw_data[] = $payment;
			$raw_data[] = $balance;
			
			$sortable_data[] = $raw_data;	
		}
		
		return $sortable_data;
	}
	
	/*
	open a supplier account
	*/
	function open_account($supplier_id=-1)
	{
		$sortable_data = $this->get_account($supplier_id);
		//Supplier info
		$supplier_info = $this->Supplier->get_info($supplier_id);
			//$supplier_info->first_name;
			
		$supplier_name = $supplier_info->first_name;
			if(empty($supplier_name))
			{
				$supplier_name = '[None]';
			}
		
		//Format in table
		$data['supplier_name']=$supplier_name;
		$data['supplier_id']=$supplier_id;
		$data['controller_name']=strtolower($this->uri->segment(1));
		$data['form_width']=$this->get_form_width();
		$data['manage_table']=supplier_acct_order_table($sortable_data,$this);
		$this->load->view("suppliers/supplier_orders",$data);
	}
	
	/*
	open a supplier account
	*/
	function refresh_account($supplier_id=-1)
	{
		$sortable_data = $this->get_account($supplier_id);		
		$data_rows = supplier_acct_order_table_data_rows($sortable_data,$this);
		echo $data_rows;
	}
	
	
	///////////// delete orders //////////////////////
	function delete_order()
	{
		$order_to_delete=$this->input->post('ids');
		
		if($this->Item->delete_orders_data($order_to_delete))
		{
			echo json_encode(array('success'=>true,'message'=>$this->lang->line('items_successful_deleted').' '.
			count($order_to_delete).' '.$this->lang->line('items_one_or_multiple')));
		}
		else
		{
			echo json_encode(array('success'=>false,'message'=>$this->lang->line('items_cannot_be_deleted')));
		}
	}
	
	
	////Payments Made for an Order////
	function order_payment_made($order_id)
	{
		$payment_result = $this->Item->order_payment_made($order_id);
		$data['manage']= order_payment_table($payment_result,$this);				
		$this->load->view("items/orders_payments", $data);
	}
	
	////viewing the purchases for an order////////////////
	function view_order_purchases($order_id)
	{
		$order_info = $this->Item->get_order_info($order_id);
		$query_order_items = print_order_items_manage_table($order_info['query_order_items'],$this);
		$data_view['manage']=$query_order_items;
		$this->load->view("suppliers/view_order_purchases", $data_view);
		
		return;
	}
	
	//Retrieve the  order for printing
	function print_order($order_id)
	{
		//$return_id=$this->input->post('supplier_print');
		$this->get_order_printing($order_id);
		
		return;
	}
	
	//Retrieve the just saved order for printing
	function get_order_printing($return_id)
	{
		$order_info = $this->Item->get_order_info($return_id);
		$item = $order_info['query_order']->row();
		$supplier_info = $this->Supplier->get_info($item->supplier_id);
		$query_order_items = print_order_items_manage_table($order_info['query_order_items'],$this);
		$data_view['order_date']=$item->order_date;
		$data_view['first_name']=$supplier_info->first_name;
		$data_view['last_name']=$supplier_info->last_name;
		$data_view['manage']=$query_order_items;
				
		$this->load->view("items/print_order", $data_view);
		
		return;
	}
	
	////Stament of Account////////////////
	function statement_of_account($supplier_id)
	{
		$sortable_data = $this->get_account($supplier_id);
		$supplier_info = $this->Supplier->get_info($supplier_id);
			//$supplier_info->first_name;
			
		$supplier_name = $supplier_info->first_name;
			if(empty($supplier_name))
			{
				$supplier_name = '[None]';
			}
		
		//Format in table
		$data['supplier_id']=$supplier_id;
		$data['supplier_name']=$supplier_name;   	 	 	 
		$data['phone_number']=$supplier_info->phone_number;
		$data['email']=$supplier_info->email;
		$data['address_1']=$supplier_info->address_1;
		$data['address_2']=$supplier_info->address_2;
		$data['account_number']=$supplier_info->account_number;		
		$data['controller_name']=strtolower($this->uri->segment(1));
		$data['form_width']=$this->get_form_width();
		$data['manage']=statement_acct_order_table($sortable_data,$this);
		$this->load->view("suppliers/print_sate_acct",$data);
		
		return;
	}
	
}
?>